'use strict';

define(['angular', 'moment', 'angular-ui-router', '../../../common/utils/service', 'infiniteScroll', 'angular-material'], function (angular, moment) {
	var module = angular.module('memberPoints', ['ui.router', 'dataService', 'infiniteScroll', 'ngMaterial']);
	module.config(['$stateProvider', function ($stateProvider) {
		$stateProvider
			.state("member.points", {
				url: '/points',
				views: {
					"@": {
						controller: 'MemberPointsCtrl',
						templateUrl: '/mstore/member/points/points.html'
					}
				}
			})
		$stateProvider
			.state("member.oldPoints", {
				url: '/member-points',
				views: {
					"@": {
						controller: 'MemberOldPointsCtrl'
					}
				}
			})
		$stateProvider
			.state("member.points.details", {
				url: '/details/:id',
				views: {
					"@": {
						controller: 'MemberPointsDetailsCtrl',
						templateUrl: '/mstore/member/points/details.html'
					}
				}
			})
	}]);
//TODO 以后会删除
	module.controller('MemberOldPointsCtrl', ['$state', function ($state) {
		$state.transitionTo('member.points')
	}])
	module.controller('MemberPointsCtrl', ['$rootScope', '$state', '$scope', 'dataService', function ($rootScope, $state, $scope, dataService) {
		$rootScope.pageTitle = "积分记录";

		$scope.memberBound(function () {
			var month = moment().format("YYYY-MM");
			$scope.tabs = [{name: '全部', type: 'all'}, {name: '积分', type: 'add'}, {name: '支付', type: 'pay'}]
			$scope.$watch("selectedIndex", function (newValue, oldValue) {
				if (newValue != oldValue)
					$scope.changeType($scope.tabs[newValue].type);
			});
			function getAccountList(type, statusKey, monthDate, cb) {
				if ($scope[statusKey]) {
					return;
				}

				$scope[statusKey] = true;
				dataService.getPointsData(sessionStorage.memberId, type, monthDate, function (err, dataObj) {
					if (err) {
						$scope[statusKey] = false;
						cb && cb(err);
						return;
					}

					$scope.dataObj = dataObj;
					$scope.dataObj.month = dataService.generateMonthData(month, dataObj.txList, "txList", 'txtime');
					$scope[statusKey] = false;
					cb && cb(null, dataObj.txList.length);
				})
			}

			$scope.type = "all";
			getAccountList($scope.type, 'loadingMonthStatus', month);
			$scope.changeType = function (type) {
				getAccountList(type, 'loadingMonthStatus', month);
				$scope.type = type;
			}
			$scope.loadMore = function (cb) {
				month = moment(month).subtract(1, "month").format("YYYY-MM");
				getAccountList($scope.type, 'loadingMoreMonthStatus', month, cb);
			}

			$scope.choseItem = function (item) {
				$state.transitionTo("member.points.details", {id: item.id});
			}
		}, {force: true})

	}]);
	module.controller('MemberPointsDetailsCtrl', ['$state', '$rootScope', '$scope', 'dataService', '$stateParams', function ($state, $rootScope, $scope, dataService, $stateParams) {
		if (!$stateParams.id) {
			return $state.transitionTo("member.points");
		}
		$rootScope.pageTitle = "积分详情";
		dataService.getMemberOneInfo(sessionStorage.memberId, function (err, dataObj) {
			if (err) return;
			$scope.dataObj = dataObj;
			dataService.getOnePointsTx($stateParams.id, function (err, data) {
				if (err) {
					$rootScope.showMessage(data.error);
					return;
				}
				$scope.detail = data;
			})
		})
	}]);
	module.filter('pointsTxType', function () {
		return function (tx) {
			if (!tx) return;
			if (tx == "reward") {
				return "消费积分";
			} else if (tx == 'manual-update') {
				return "人工设置"
			} else if (tx == 'revert') {
				return "支付撤销";
			} else if (tx == "pay") {
				return "支付";
			} else if (tx == "refund") {
				return "退货反扣";
			} else return tx;
		}
	})
});
